package com.example.ch07.preferences;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.example.ch07.R;

/**
 * Sub-activity that is executed by the redirection example when input is needed
 * from the user.
 */
public class SharedPreferencesInputActivity extends AppCompatActivity {
    private String mTextPref;
    private EditText mText;
    private OnClickListener mApplyListener = new OnClickListener() {
        public void onClick(View v) {
            SharedPreferences preferences = getSharedPreferences(
                    "RedirectData", 0);
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("text", mText.getText().toString());

            if (editor.commit()) {
                setResult(RESULT_OK);
            }

            finish();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.c07_redirect_getter);

        // Watch for button clicks.
        Button applyButton = (Button) findViewById(R.id.apply);
        applyButton.setOnClickListener(mApplyListener);

        // The text being set.
        mText = (EditText) findViewById(R.id.editText);

        // Display the stored values, or if not stored initialize with an empty
        // String
        loadPrefs();
    }

    private final void loadPrefs() {
        // Retrieve the current redirect values.
        // NOTE: because this preference is shared between multiple
        // activities, you must be careful about when you read or write
        // it in order to keep from stepping on yourself.
        SharedPreferences preferences = getSharedPreferences("RedirectData", 0);

        mTextPref = preferences.getString("text", null);
        if (mTextPref != null) {
            mText.setText(mTextPref);
        } else {
            mText.setText("");
        }
    }
}
